package routers

import (
	"gin_gorm/api"
	"gin_gorm/middleware"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	//go get github.com/gin-contrib/sessions
)

func NewRouter() *gin.Engine {
	r := gin.Default()
	store := cookie.NewStore([]byte("something-very-secret"))
	r.Use(sessions.Sessions("mysession", store))
	v1 := r.Group("api/v1")
	{
		// 用户操作
		v1.POST("user/register", api.UserRegister)
		// 用户登录
		v1.POST("user/login", api.UserLogin)
		// jwt中间件认证分组
		authed := v1.Group("/")
		authed.Use(middleware.JWT())
		{
			authed.POST("task", api.CreateTask)     // 创建备忘录
			authed.GET("task/:id", api.ShowTask)    // 展示一条备忘录
			authed.GET("tasks", api.ListTask)       // 展示所有的备忘录
			authed.PUT("task/:id", api.UpdateTask)  // 更新一条备忘录
			authed.POST("search", api.SearchTask)   // 寻找一条备忘录
			authed.POST("task/:id", api.DeleteTask) // 删除一条备忘录
		}
	}
	return r
}
